<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use app\index\model\QUser;
use think\Session;

class Index extends Controller
{
    // 用户注册
    public function register(Request $req)
    {
        $userData = $req->param();
        $userIDcard = $userData['userIDcard'];
        // $loginInfo = QUser::where("userMobile = $userMobile ")->find()->toArray();
        $loginInfo = QUser::get(['userIDcard' => $userIDcard]);
        if($loginInfo){
            return json(['status'=>0,'message'=>'用户已注册']);
        }
        else
        {
            $userIDcard = $userData['userIDcard'];
            // 注册
            $userData['userToken'] = md5($userIDcard);
            $userData['time'] = time();
            $userData['answerTimes'] = 0;
            $userData['dayScore'] = 0;
            $userData['totalScore'] = 0;
            $res = QUser::create($userData);
            if($res->id)
            {
                Session('userToken',$userData['userToken']);
                return json(['status'=>0,'message'=>'注册成功']);
            }
        }
    }

    // 登录
    public function login(Request $req)
    {
        $userData = $req->param();
        // $loginInfo = QUser::where(['userIDcard'=>$userData['userIDcard'],'userMobile'=>$userData['userMobile'],'state'=>0])->find()->toArray();
        $loginInfo = db("q_user")->where(['userIDcard'=>$userData['userIDcard'],'userMobile'=>$userData['userMobile'],'state'=>0])->find();
        // dump($loginInfo);
        if(!empty($loginInfo)){
            Session('userToken',md5($loginInfo['userIDcard']));
            return json(['status'=>0,'message'=>'登录成功']);
        }else{
            return json(['status'=>1,'message'=>'登录失败']);
        }
    }

    // 判断用户登录
    public function checklogin()
    {
        $token = empty(Session('userToken')) ? false : Session('userToken');
        if($token)
        {
            // 判断token对不对
            $result = db("q_user")->where("userToken = '$token'")->find();
            if(!empty($result) && md5($result['userIDcard']) == $token)
            {
                return json(['stats' => 0,'message'=>'已登录']);
            }
            else
            {
                return json(['stats' => 0,'message'=>'登录信息有误']);
            }
        }
        else
        {
            return json(['state'=>0,'message'=>'未登录']);
        }
    }

    // 查询奖励存量
    public function prizestock()
    {
        $stock = db('q_prize')->select();
        return json(['status'=> 0,'data'=>$stock]);
    }

    // 累计得分
    public function totalscore()
    {
        $totalScore = db('q_user')->field("userToken,totalScore")->where("state = 0 and FROM_UNIXTIME(time, '%Y%m%d') < ".date('Ymd'))->limit(30)->select();
        //$p = db('q_prize')->field('prizeOut')->where('prizeCode = 1')->select();
        //dump($p['0']['prizeOut']);
        //die();
        foreach($totalScore as $value){
            if($value['totalScore'] >= 40 && $value['totalScore'] < 80){
                $p = db('q_prize')->field('prizeOut')->where('prizeCode = 2')->select();
                // 100元一年期保险
                if($p['0']['prizeOut'] <= 900){
                    $res = db('q_reward')->where(['userToken'=>$value['userToken'],'reward'=>2])->find();
                    if($res){
                        echo "已有200元保险";
                    }else{
                        echo "200元保险";
                        db('q_reward')->insert(['userToken'=>$value['userToken'],'reward'=>2,'time'=>time()]);
                        db('q_prize')->where('prizeCode = 2')->setInc('prizeOut');
                    }
                }
            }else if( $value['totalScore'] >= 80){
                $pp = db('q_prize')->field('prizeOut')->where("prizeCode = 3")->select();
                if($pp['0']['prizeOut'] <= 450){
                   echo "300元保险";
                   $res = db('q_reward')->where(['userToken'=>$value['userToken'],'reward'=>2])->find();
                   if($res){
                       db('q_reward')->where(['userToken'=>$value['userToken'],'reward'=>2])->update(['reward'=>3,'time'=>time()]);
                       // 300元三年期的奖励，发出数加一
                       db('q_prize')->where('prizeCode = 3')->setInc('prizeOut');
                       db('q_prize')->where('prizeCode = 2')->setDec('prizeOut');
                   }else{
                       // 将100元一年期的奖励，升级为200元两年期的
                       db('q_reward')->insert(['userToken'=>$value['userToken'],'reward'=>3,'time'=>time()]);
                       // 100元一年期的奖励，发出数减一
                       db('q_prize')->where('prizeCode = 2')->setDec('prizeOut');
                       // 200元两年期的奖励，发出数加一
                       db('q_prize')->where('prizeCode = 3')->setInc('prizeOut');
                   }
               }
            }
            db('q_user')->where(['userToken'=>$value['userToken']])->update(['time'=>time()]);
        }
    }

    // 请一天数据清零
    public function dayclear()
    {
        $res = db('q_user')->where('state = 0')->update(['answerTimes' => 0,'dayScore' => 0]);
        echo $res;
    }



}
